import { defineStore } from 'pinia';
import type { UserState } from '../interface';
import piniaPersistConfig from '@/store/helper/persist';
import type { loginResponseData, loginForm } from '@/api/user/type';
import { reqLogin } from '@/api/user';
export const useUserStore = defineStore('User', {
  state: (): UserState => {
    return {
      token: '',
    };
  },
  actions: {
    async userlogin(data: loginForm) {
      const res: loginResponseData = await reqLogin(data);
      if (res.code === 200) {
        this.token = res.data.access_token;
        return 'ok';
      } else {
        return Promise.reject(new Error(res.message));
      }
    },
  },
  persist: piniaPersistConfig('user-access-token'),
});
